﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Manager;
using Model;
using Manager.CustomerHelper;
namespace HealthSystem.sysAdmin
{
    public partial class CtityAdmin_New : BasePage
    {
        public override ModuleType Module
        {
            get
            {
                return ModuleType.SchoolManager;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnClose.OnClientClick = ExtAspNet.ActiveWindow.GetHidePostBackReference();
                triggDistrict.OnClientTriggerClick = Window1.GetSaveStateReference(txtDistrictid.ClientID,triggDistrict.ClientID)
                   + Window1.GetShowReference("~/ChooseDistrict.aspx", "地区选择");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string usertype = rdusertype.SelectedValue;

            HW_Admin_Detail adminDetail = new HW_Admin_Detail();
            adminDetail.XX_UserName = txtusername.Text.Trim();
            adminDetail.MobilePhone = txtMobile.Text.Trim();
            adminDetail.OfficePhone = txtPhone.Text.Trim();
            adminDetail.WorkUnit = unit.Text.Trim();
            adminDetail.Email = txtEmail.Text.Trim();
            adminDetail.Version = 0;
            HW_Manger_Admin_User adminUser = new HW_Manger_Admin_User();
            adminUser.XX_UserName = txtusername.Text.Trim();
            GetUserType(usertype,ref adminUser);
            adminUser.Version = 0;
            string pass = SecrityPass.CreatePassword(6);
            adminUser.XX_Pass =SecrityPass.GetPassword(pass);
            adminUser.CNName = cnname.Text.Trim();
            adminUser.hW_Admin_Detail=adminDetail;
            //明文密码
            TShowPassWord entity=new TShowPassWord ();
            entity.xx_UserName=txtusername.Text.Trim();
            entity.xx_password=pass;
            try
            {
                HW_Manger_Admin_UserManager adminUserManager = new HW_Manger_Admin_UserManager();
                adminUserManager.Add(adminUser, entity);
                ExtAspNet.Alert.Show("增加管理员成功");
            }
            catch
            {
                ExtAspNet.Alert.Show("增加管理员出错");
            }

        }
        private HW_Manger_Admin_User GetUserType(string userType,ref HW_Manger_Admin_User adminUser)
        {
            HW_DistrictManager districtManager = new HW_DistrictManager();
            var d= districtManager.GetById(txtDistrictid.Text);
            switch (userType)
            {
                case "x":
                    if (d.Level == 3)
                    {
                        adminUser.RoleId = 1002;
                        adminUser.CityId = txtDistrictid.Text.Trim();
                        adminUser.DistrictCode = txtDistrictid.Text.Trim();
                    }
                    else if (d.Level == 4)
                    {
                        adminUser.RoleId = 1003;
                        adminUser.DistrictCode = txtDistrictid.Text.Trim();
                        adminUser.CityId = d.ParentDistrictCode;
                    }
                    break;
                case "b":
                    if (d.Level == 3)
                    {
                        adminUser.RoleId = 1011;
                        adminUser.CityId = txtDistrictid.Text.Trim();
                        adminUser.DistrictCode = txtDistrictid.Text.Trim();
                    }
                    else if (d.Level == 4)
                    {
                        adminUser.RoleId = 1012;
                        adminUser.DistrictCode = txtDistrictid.Text.Trim();
                        adminUser.CityId = d.ParentDistrictCode;
                    }
                    break;
            }
            return adminUser;
        }
        
    }
}